/*
  FileName : ISP1761_HCD.h

  Created on 2007.10.17 by ZZP.

  Revision History:

*/

#ifndef __ISP1761_HCD_H__
#define __ISP1761_HCD_H__

#include "Usb_Descriptor.h"

#define HC_PAYLOAD_ADDR 0x1000//((0x1000 - 0x400) >> 3)

#define HC_CONTROL_TT     0x00
#define HC_BULK_TT        0x02

#define CONTROL_PTD       0x00
#define BULK_IN_PTD       0x01
#define BULK_OUT_PTD      0x02

#define EP_CONTROL_TYPE   0x00
#define EP_BULK_TYPE      0x02

#define HC_OUT_TOKEN      0x00
#define HC_IN_TOKEN       0x01
#define HC_SETUP_TOKEN    0x02
#define HC_PING_TOKEN     0x03

#define HC_DEV_EP0        0x00

#define PID_DATA0         0x00
#define PID_DATA1         0x01


#define PORT_TYPE_INTERNAL_HUB  0x00
#define PORT_TYPE_EXTERNAL_HUB  0x01
#define PORT_TYPE_DEVICE        0x02

#define HIGH_SPEED_DEVICE       0x02
#define FULL_SPEED_DEVICE       0x01
#define LOW_SPEED_DEVICE        0x00

#define USB_ERR_V               0x0001
#define USB_ERR_X               0x0002
#define USB_ERR_HALT            0x0004
#define USB_ERR_ACTIVE          0x0008
#define USB_ERR_NAK             0x0010

#define HC_DATA_STATE_IDLE      0
#define HC_DATA_STATE_CBW       1
#define HC_DATA_STATE_DATA      2
#define HC_DATA_STATE_CSW       3

struct HC_XDEVICE_INFO{
  Uns DevOK;
  Uns CtrlXferOK;
  Uns Connected;
  Uns IsHub;
  Uns State;
  Uns MaxLUN;
  Uns CurrLUN;
  Uns Ctrl_MaxPktSz;
  Uns BulkIn_MaxPktSz;
  Uns BulkOut_MaxPktSz;
  Uns CtrlTog;
  Uns BulkInTog;
  Uns BulkOutTog;
  Uns DevAddr;
  Uns CfgValue;
  Uns BulkInEP;
  Uns BulkOutEP;
  Uns Speed;
  Uns ATLBufDone;
  Uns DataState;
  Uns *DataPtr;
  LgUns TxTotBytes;
  Uns TxComplete;

  Uns XHubOK;

  LgUns Sec0LBA;
  LgUns NextRecLBA;
  Uns RecCnt;

  ///////////////////////////////
  LgUns LBA_Xfer;
  Uns LBA_XferLen;
  Uns AllocLen;
  Uns PageControl;
  Uns PageCode;
  Uns SubPageCode;
  Uns DESC;
  LgUns HcCBW_wXferLen;
  Uns HcCBW_OpCode;
  ///////////////////////////////

  struct DEVICE_DESCRIPTOR_STRUCT DevDesc;
  struct DEVICE_QUALIFIER_STRUCT DevQlf;
  struct CONFIG_DESCRIPTOR_STRUCT CfgDesc;
  struct CONFIG_DESCRIPTOR_STRUCT HiSpdCfgDesc;
  struct INTERFACE_DESCRIPTOR_STRUCT IntfDesc;
  struct ENDPOINT_DESCRIPTOR_STRUCT EPDesc[4];
  STRING_DESCRIPTOR_STRUCT StrDesc;
  REQUEST_CMD_STRUCT ReqCmd;
};

extern volatile struct HC_XDEVICE_INFO Hc_XDevInfo;

extern Uns BulkPayLoadBuf[0x1000];

struct PORT_STATUS_BITS{
  Uns ConnectSts  : 1;
  Uns PortEnabled : 1;
  Uns Suspend     : 1;
  Uns OverCurrent : 1;
  Uns Reset       : 1;
  Uns rsvd1       : 3;
  Uns Power       : 1;
  Uns LSDevAttach : 1;
  Uns HSDevAttach : 1;
  Uns TestMode    : 1;
  Uns Indicator   : 1;
  Uns rsvd2       : 3;
};
union PORT_STATUS_REG{
  Uns all;
  struct PORT_STATUS_BITS bit;
};

struct PORT_STATUS_CHANGE_BITS{
  Uns ConnectStsChange : 1;
  Uns PortEnableChange : 1;
  Uns SuspendChange    : 1;
  Uns OverCurrentChange : 1;
  Uns ResetChange      : 1;
  Uns rsvd             : 11;
};
union PORT_STATUS_CHANGE_REG{
  Uns all;
  struct PORT_STATUS_CHANGE_BITS bit;
};

struct PORT_STATUS_DATA{
  Uns CtrlXferOK;
  Uns Ctrl_MaxPktSz;
  Uns ATLBufDone;
  struct DEVICE_DESCRIPTOR_STRUCT DevDesc;
  struct HUB_DESC_REG HubDesc;
  struct CONFIG_DESCRIPTOR_STRUCT CfgDesc;
  struct INTERFACE_DESCRIPTOR_STRUCT IntfDesc;
  union PORT_STATUS_REG Sts;
  union PORT_STATUS_CHANGE_REG StsChange;
};

extern volatile struct PORT_STATUS_DATA PortStsData2;

extern void Hc_InitHardware(void);
extern void Hc_EnumInternalHub(void);
extern void Hc_GetInternalPortStatus(Uns Port);
extern void Hc_GetInternalHubStatus(void);
extern void Hc_EnumDevice(void);
extern void Hc_EnumHub(Uns HubType);
extern void Hc_PortEnable(void);
extern void Hc_GetDeviceDescriptor(Uns Addr);
extern void Hc_GetDeviceQualifier(Uns Addr);
extern void Hc_GetCfgDescriptor(Uns Addr);
extern void Hc_GetStrDescriptor(Uns Addr);
extern void Hc_SetAddress(Uns Old_Addr, Uns New_Addr);
extern void Hc_SetConfig(Uns Addr,Uns Config);
extern void Hc_SetInterface(Uns Addr, Uns Interface);
extern Uns Hc_GetStatus(Uns Addr,Uns FeatureSel, Uns EP);
extern void Hc_ClearFeature(Uns Addr,Uns Type,Uns EP);
extern void Hc_ResumeDC(Uns Addr);
extern void Mass_GetMaxLUN(Uns Addr);
extern void Mass_BulkReset(Uns Addr);

extern void Hc_MakePTD(Uns TransType, Uns Token, Uns PktSize, 
                       Uns EP, Uns TotBytes, Uns Tog, Uns DevAddr, Uns DataAddr);
extern void Hc_SendPTD(Uns TransType, Uns Token, Uns PTDType,
                       Uns EP, Uns TotBytes, Uns Tog, Uns DevAddr, Uns DataAddr);
extern void Hc_SetupToken(Uns Addr, Uns wReqTypeReq, Uns wValue, Uns wIndex, Uns wLen, Uns PortType);
extern void Hc_AckToken(Uns Addr, Uns TokenInOut, Uns PortType);
extern void Hc_InToken(Uns Addr, Uns *Dest, Uns TotLen, Uns PortType); 
extern void Hc_CheckCtrlErr(Uns PortType);
extern void Hc_WritePayLoad(Uns AddrOffset, Uns *Src, Uns TotBytes);
extern void Hc_ReadPayLoad(Uns AddrOffset, Uns *Dest, Uns TotBytes);
extern Uns Hc_GetErrCode(Uns *Tog);
extern Uns Hc_GetXferLen(void);

extern void Hub_ClearFeature(Uns Addr, Uns FeatureSel, Uns PortType);
extern void Hub_ClearPortFeature(Uns Addr, Uns FeatureSel, Uns Sel, Uns Port, Uns PortType);
extern void Hub_ClearTTBuffer(Uns Addr, Uns EP, Uns TTPort, Uns PortType);
extern void Hub_GetDescriptor(Uns Addr, Uns DescType, Uns Len, Uns *Dest, Uns PortType);
extern void Hub_GetStatus(Uns Addr, Uns *Dest, Uns PortType);
extern void Hub_GetPortStatus(Uns Addr, Uns Port, Uns *Dest, Uns PortType);
extern void Hub_ResetTT(Uns Addr, Uns Port, Uns PortType);
extern void Hub_SetDescriptor(Uns Addr, Uns Type, Uns Index, Uns LID, Uns Len, Uns *Src, Uns PortType);
extern void Hub_SetFeature(Uns Addr, Uns FeatureSel, Uns PortType);
extern void Hub_SetPortFeature(Uns Addr, Uns FeatureSel, Uns Sel, Uns Port, Uns PortType);
extern void Hub_GetTTState(Uns Addr, Uns Flags, Uns Port, Uns Len, Uns *Dest, Uns PortType);
extern void Hub_StopTT(Uns Addr, Uns Port, Uns PortType);


















#endif

